#!/usr/bin/perl -w # usefule for syntax highlighting
use gri_secmgr qw(get_user_name);
use gri_report_config;

sub mm_report_chooser($) {
my $action=$_[0];

	my $user_name=get_user_name();
	my ($user_admin,$err)=$::GRI_FRONTEND->does_user_have_role($user_name,'user_admin');
	if(!$user_admin) {
		return "You do not have permissions to access this page.";
	}
	my $rep_dir=$::GRI_FRONTEND->get_global_directory("reports");
	my $dfd;
	@R=();
	opendir($dfd,$rep_dir) or return("ERROR: Can not open directory '$rep_dir': $!");
	while($cline=readdir($dfd)) {
		next if ! -f "$rep_dir/$cline";
		next if $cline !~ /\.xml$/;
		push @R,$cline;
	}
	closedir($dfd);

	# For each of the reports load in the details ...

	my @R2=map {s/\.xml$//; $_;} @R;
	@R=sort {$::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b)} @R2;
	@R=map {"$_.xml";} @R;
	my $s="";
	$row=0;
	for my $crep (@R) {
		my $x2=$crep;
		$x2 =~ s/\.xml$//;
		my @info=$::GRI_FRONTEND->get_portal_report_details("__DUMMY__",$x2);
		next if !defined($info[0]);
		my $class="gri_odd";
		$class="gri_even" if $row%2==0;
		$s.="<tr id=$class><td class=gri_cell>$info[0]</td><td class=gri_cell-right>$info[5]</td>";
		my $r2=string_to_hex($x2);
		my $c2=string_to_hex($info[5]);
		$s.="<td class=gri_cell-right>";
		if($action eq "Delete") {
			$s.=link_to_str("stdlink","Delete","form_delete_report_confirm","repname=$r2,count=$c2");
		} elsif($action eq "Modify") {
			$s.=link_to_str("stdlink","Modify","form_modify_report","rep_name=$r2");
		}
		$s.="</td></tr>\n";
		$row++;
	}
	return $s;
}

